# -*- coding:utf-8 -*-

from tornado.web import Application, RequestHandler, url
from tornado.ioloop import IOLoop
from tornado.httpserver import HTTPServer
from tornado.options import options,define
import tornado.options


define("port", default=8888, type=int)

class IndexHandler(RequestHandler):

   def set_default_headers(self):
        # 第二种响应头设置方式
        print("---------> 响应头set_default_headers()执行")
        self.set_header("Content-type", "application/json; charset=utf-8")
        self.set_header("js", "zj")
   def get(self):
        # 第一种操作响应头的方式：
        # self.set_header("Content-type", "application/json")
        print("---------->get方法执行")
        self.write("{'简书':'知几'}")
        self.set_header("jianshu", "zhiji")

class IndexHandler2(RequestHandler):

    def get(self):
        self.write("hello jianshu.com")
        self.set_cookie("loginuser", "admin")
        print(self.get_cookie("loginuser"))
        print(self.cookies)


class RegistHandler(RequestHandler):
   def initialize(self, title):
       self.title = title

   def get(self):
       self.write("注册业务处理:" + str(self.title))


class LoginHandler(RequestHandler):
   def get(self):
       self.write("用户登录页面展示")

   def post(self):
      self.write("用户登录功能处理")


if __name__ == "__main__":
   app = Application(
       [
           (r"/", IndexHandler),
           (r"/2",IndexHandler2),
           (r"/regist", RegistHandler, {"title": "会员注册"}),
           url(r"/login", LoginHandler, name="login"),
       ]
   )

   tornado.options.parse_command_line()

   http_server = HTTPServer(app)
   http_server.listen(8888)

   IOLoop.current().start()